package com.sikao.app.utils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by y50 on 2016/4/15.
 */
public class SharepreferenceUtil {

	private static SharedPreferences sharedPreferences;
	private static SharepreferenceUtil sp;
	private static SharedPreferences.Editor editor;
	private static String spName;

	private SharepreferenceUtil(String name) {
		spName = name;
	}

	public static SharepreferenceUtil getInstance(Context context){
		if(sp == null){
			sp = new SharepreferenceUtil("Preferences");
			sharedPreferences = context.getSharedPreferences(spName,Activity.MODE_PRIVATE);
			editor = sharedPreferences.edit();
		}
		return  sp;
	}

	public void putString(String key, String value) {
		editor.putString(key, value);
		editor.commit();
	}


	public String getString(String key){
		String result = null;
		result = sharedPreferences.getString(key, "");
		return result;
	}

	public int getInt(String key){
		int result = -1;
		result = sharedPreferences.getInt(key, -1);
		return result;
	}

	public boolean getBoolean(String key){
		boolean result = false;
		result = sharedPreferences.getBoolean(key, false);
		return result;
	}

	public void commitString(String key,String value){
		editor.putString(key, value);
		editor.commit();
	}

	public void commitInt(String key,int value){
		editor.putInt(key, value);
		editor.commit();
	}

	public void commitBoolean(String key,boolean value){
		editor.putBoolean(key, value);
		editor.commit();
	}

	public void clear(){
		editor.clear().commit();
	}
}
